<template>
   <div class="app-container">
      <el-table v-loading="loading" :data="annList">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="消息内容" align="center" prop="title" />
         <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:acitvity:edit']">修改</el-button>
            </template>
         </el-table-column>
      </el-table>



      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
         <el-form ref="annRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="公告内容" prop="title">
               <el-input v-model="form.title" placeholder="请输入公告内容" />
            </el-form-item>

         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Announcement">
import { getMessageList,  updateMessage } from "@/api/system/message";

const { proxy } = getCurrentInstance();

const annList = ref([]);
const open = ref(false);
const loading = ref(true);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 1,
  },
  rules: {
    title: [{ required: true, message: "消息不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  getMessageList().then(response => {
   annList.value = response.data.records;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    title: undefined,
    sort: undefined,
    status: "0",
  };
  proxy.resetForm("annRef");
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const actId = row.id ;
  getMessageList().then(response => {
   if(response.data.records.length==0){
    proxy.$modal.msgWarning("请先添加公告");
    return;    
   }
    form.value = response.data.records[0];
    open.value = true;
    title.value = "修改公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["annRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
         updateMessage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
      }
  });
}


getList();
</script>
